home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / cool.lha / ice / pisces / rcs / maketime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-04  |  7.1 KB  |  245 lines

  1. #
  2. /*
  3.  * MAKETIME        derive 32-bit time value from TM structure.
  4.  *
  5.  * Usage:
  6.  *    long t,maketime();
  7.  *    struct tm *tp;    Pointer to TM structure from <time.h>
  8.  *            NOTE: this must be extended version!!!
  9.  *    t = maketime(tp);
  10.  *
  11.  * Returns:
  12.  *    0 if failure; parameter out of range or nonsensical.
  13.  *    else long time-value.
  14.  * Notes:
  15.  *    This code is quasi-public; it may be used freely in like software.
  16.  *    It is not to be sold, nor used in licensed software without
  17.  *    permission of the author.
  18.  *    For everyone's benefit, please report bugs and improvements!
  19.  *     Copyright 1981 by Ken Harrenstien, SRI International.
  20.  *    (ARPANET: KLH @ SRI)
  21.  */
  22. #ifndef lint
  23. static char rcsid[]= "$Id: maketime.c,v 1.1 90/05/21 13:42:54 neath Exp $";
  24. #endif
  25. /* $Log:    maketime.c,v $
  26.  * Revision 1.1  90/05/21  13:42:54  neath
  27.  * Initial revision
  28.  * 
  29.  * Revision 1.8  88/11/08  13:54:53  narten
  30.  * allow negative timezones (-24h <= x <= 24h)
  31.  * 
  32.  * Revision 1.7  88/11/08  12:02:24  narten
  33.  * changes from  eggert@sm.unisys.com (Paul Eggert)
  34.  * 
  35.  * Revision 1.7  88/08/28  14:47:52  eggert
  36.  * Allow cc -R.  Remove unportable "#endif XXX"s.
  37.  * 
  38.  * Revision 1.6  87/12/18  17:05:58  narten
  39.  * include rcsparam.h
  40.  * 
  41.  * Revision 1.5  87/12/18  11:35:51  narten
  42.  * maketime.c: fixed USG code - you have tgo call "tzset" in order to have
  43.  * "timezone" set. ("localtime" calls it, but it's probably better not to 
  44.  * count on "localtime" having been called.)
  45.  * 
  46.  * Revision 1.4  87/10/18  10:26:57  narten
  47.  * Updating version numbers. Changes relative to 1.0 are actually 
  48.  * relative to 1.2
  49.  * 
  50.  * Revision 1.3  87/09/24  13:58:45  narten
  51.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  52.  * warnings)
  53.  * 
  54.  * Revision 1.2  87/03/27  14:21:48  jenkins
  55.  * Port to suns
  56.  * 
  57.  * Revision 1.1  84/01/23  14:50:04  kcs
  58.  * Initial revision
  59.  * 
  60.  * Revision 1.2  83/12/05  10:12:56  wft
  61.  * added cond. compilation for USG Unix; long timezone;
  62.  * 
  63.  * Revision 1.1  82/05/06  11:38:00  wft
  64.  * Initial revision
  65.  * 
  66.  */
  67.  
  68.  
  69. #include "rcsbase.h"
  70. #include "time.h"
  71.  
  72. int daytb[] = {   /* # days in year thus far, indexed by month (0-12!!) */
  73.     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
  74. };
  75.  
  76. struct tm *localtime();
  77. long    time();
  78.  
  79. long maketime(atm)
  80. struct tm *atm;
  81. {    register struct tm *tp;
  82.     register int i;
  83.     int year, yday, mon, day, hour, min, sec, zone, dst, leap;
  84.     long tres, curtim;
  85.  
  86.     VOID time(&curtim);
  87.     tp = localtime(&curtim);        /* Get breakdowns of current time */
  88.     year = tp->tm_year;        /* Use to set up defaults */
  89.     mon = tp->tm_mon;
  90.     day = tp->tm_mday;
  91.  
  92.  
  93. #ifdef DEBUG
  94. printf("first YMD: %d %d %d, T=%ld\n",year,mon,day,tres);
  95. #endif
  96.     tp = atm;
  97.  
  98.     /* First must find date, using specified year, month, day.
  99.      * If one of these is unspecified, it defaults either to the
  100.      * current date (if no more global spec was given) or to the
  101.      * zero-value for that spec (i.e. a more global spec was seen).
  102.      * Start with year... note 32 bits can only handle 135 years.
  103.      */
  104.     if(tp->tm_year != TMNULL)
  105.       {    if((year = tp->tm_year) >= 1900)    /* Allow full yr # */
  106.               year -= 1900;            /* by making kosher */
  107.         mon = 0;        /* Since year was given, default */
  108.         day = 1;        /* for remaining specs is zero */
  109.       }
  110.     if(year < 70 || 70+134 < year )    /* Check range */
  111.         return(0);        /* ERR: year out of range */
  112.     leap = year&03 ? 0 : 1;        /* See if leap year */
  113.     year -= 70;            /* UNIX time starts at 1970 */
  114.  
  115.     /*
  116.      * Find day of year.
  117.      * YDAY is used only if it exists and either the month or day-of-month
  118.      * is missing.
  119.      */
  120.     if (tp->tm_yday != TMNULL
  121.      && (tp->tm_mon == TMNULL || tp->tm_mday == TMNULL))
  122.         yday = tp->tm_yday;
  123.     else
  124.       {    if(tp->tm_mon  != TMNULL)
  125.           {    mon = tp->tm_mon;    /* Month was specified */
  126.             day = 1;        /* so set remaining default */
  127.           }
  128.         if(mon < 0 || 11 < mon) return(0);    /* ERR: bad month */
  129.         if(tp->tm_mday != TMNULL) day = tp->tm_mday;
  130.         if(day < 1
  131.          || (((daytb[mon+1]-daytb[mon]) < day)
  132.             && (day!=29 || mon!=1 || !leap) ))
  133.                 return(0);        /* ERR: bad day */
  134.         yday = daytb[mon]    /* Add # of days in months so far */
  135.           + ((leap        /* Leap year, and past Feb?  If */
  136.               && mon>1)? 1:0)    /* so, add leap day for this year */
  137.           + day-1;        /* And finally add # days this mon */
  138.  
  139.                 if (tp->tm_yday != TMNULL       /* Confirm that YDAY correct */
  140.                  && tp->tm_yday != yday) return(0);     /* ERR: conflict */
  141.       }
  142.     if(yday < 0 || (leap?366:365) <= yday)
  143.         return(0);        /* ERR: bad YDAY or maketime bug */
  144.  
  145.     tres = year*365            /* Get # days of years so far */
  146.         + ((year+1)>>2)        /* plus # of leap days since 1970 */
  147.         + yday;            /* and finally add # days this year */
  148.  
  149.         if((i = tp->tm_wday) != TMNULL) /* Check WDAY if present */
  150.                 if(i < 0 || 6 < i       /* Ensure within range */
  151.                   || i != (tres+4)%7)   /* Matches? Jan 1,1970 was Thu = 4 */
  152.                         return(0);      /* ERR: bad WDAY */
  153.  
  154. #ifdef DEBUG
  155. printf("YMD: %d %d %d, T=%ld\n",year,mon,day,tres);
  156. #endif
  157.     /*
  158.      * Now determine time.  If not given, default to zeros
  159.      * (since time is always the least global spec)
  160.      */
  161.     tres *= 86400L;            /* Get # seconds (24*60*60) */
  162.     hour = min = sec = 0;
  163.     if(tp->tm_hour != TMNULL) hour = tp->tm_hour;
  164.     if(tp->tm_min  != TMNULL) min  = tp->tm_min;
  165.     if(tp->tm_sec  != TMNULL) sec  = tp->tm_sec;
  166.     if( min < 0 || 60 <= min
  167.      || sec < 0 || 60 <= sec) return(0);    /* ERR: MS out of range */
  168.     if(hour < 0 || 24 <= hour)
  169.         if(hour != 24 || (min+sec) !=0)    /* Allow 24:00 */
  170.             return(0);        /* ERR: H out of range */
  171.  
  172.     /* confirm AM/PM if there */
  173.     switch(tp->tm_ampm)
  174.       {    case 0: case TMNULL:    /* Ignore these values */
  175.             break;
  176.         case 1:            /* AM */
  177.         case 2:            /* PM */
  178.             if(hour > 12) return(0);  /* ERR: hrs 13-23 bad */
  179.             if(hour ==12) hour = 0;    /* Modulo 12 */
  180.             if(tp->tm_ampm == 2)    /* If PM, then */
  181.                 hour += 12;    /*   get 24-hour time */
  182.             break;
  183.         default: return(0);    /* ERR: illegal TM_AMPM value */
  184.       }
  185.  
  186.     tres += sec + 60L*(min + 60L*hour);    /* Add in # secs of time */
  187.  
  188. #ifdef DEBUG
  189. printf("HMS: %d %d %d T=%ld\n",hour,min,sec,tres);
  190. #endif
  191.     /*
  192.      * We now have the GMT date/time and must make final
  193.      * adjustment for the specified time zone.  If none is specified,
  194.      * the local time-zone is assumed.
  195.      */
  196.     if((zone = tp->tm_zon) == TMNULL    /* If unspecified */
  197.      || (zone == 1))            /* or local-zone requested */
  198.         zone = localzone();        /* then set to local zone */
  199.     if(zone < -24*60 || 24*60 <= zone)
  200.         return(0);            /* ERR: zone out of range */
  201.  
  202.     /* See if must apply Daylight Saving Time shift.
  203.      * Note that if DST is specified, validity is not checked.
  204.      */
  205.     if((dst = tp->tm_isdst) == TMNULL)    /* Must we figure it out? */
  206.       {    curtim = tres +localzone()*60L;    /* Yuck.  Get equiv local */
  207.         dst = localtime(&curtim)->tm_isdst;     /* time, and ask. */
  208.       }
  209.     tres += zone*60L -(dst?3600:0);    /* Add in # seconds of zone adj */
  210.  
  211.     return(tres);
  212. }
  213.  
  214.  
  215. /* LOCALZONE        return local timezone in # mins west of GMT
  216.  *
  217.  */
  218.  
  219. #if defined(V6) || defined(USG)
  220. extern long timezone;
  221. #else
  222. #include <sys/types.h>
  223. #include <sys/timeb.h>
  224. #endif
  225.  
  226. static int lclzon;
  227. localzone()
  228. {
  229.     if (!lclzon) {
  230. #if defined(V6) || defined(USG)
  231. #ifdef USG
  232.     tzset();
  233. #endif
  234.     lclzon = timezone/60 + 1;
  235. #else
  236.     struct timeb tb;
  237.  
  238.     ftime(&tb);
  239.     lclzon = tb.timezone + 1;
  240.  
  241. #endif
  242.     }
  243.     return lclzon - 1;
  244. }
  245.